package com.food.restaurantpro.connection;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.protocol.HTTP;

import com.food.restaurantpro.json.JsonReader;
import com.food.restaurantpro.json.JsonToken;
import com.food.restaurantpro.objects.AboutUs;
import com.food.restaurantpro.objects.CurrentTime;
import com.food.restaurantpro.objects.DishCategory;
import com.food.restaurantpro.objects.Food;
import com.food.restaurantpro.objects.FoodForUpdate;
import com.food.restaurantpro.objects.Place;
import com.food.restaurantpro.objects.PreOrderObject;
import com.food.restaurantpro.objects.Promotion;
import com.food.restaurantpro.objects.PromotionForUpdate;
import com.food.restaurantpro.objects.Reservation;
import com.food.restaurantpro.objects.UserInfo;

import android.util.Log;

public class ParserDataHelper implements IServerDataParser {
	private static final String TAG = "ParserDataHelper";
	private volatile static ParserDataHelper uniqueInstance;

	public static ParserDataHelper getInstance() {
		if (uniqueInstance == null) {
			synchronized (ParserDataHelper.class) {
				if (uniqueInstance == null) {
					uniqueInstance = new ParserDataHelper();
				}
			}
		}
		return uniqueInstance;
	}

	/**
	 * Parse json string
	 * 
	 * @param data
	 * @param method
	 * @return All parser method return object ApiServiceResult
	 */
	public DataResult parse(InputStream data, MethodParams methodparam) {
		if (data == null) {
			return null;
		}

		JsonReader reader;
		try {
			reader = new JsonReader(new InputStreamReader(data, HTTP.UTF_8));
			reader.setLenient(true);
			switch (methodparam.getMethod()) {
			case getAllPromotions:
				return parseGetAllPromotions(reader);
			case getCurrentTime:
				return parseGetCurrentTime(reader);
			case getUpdatePromotion:
				return parseGetUpdatePromotion(reader);
			case getAllDishCategory:
				return parseGetAllDishCategory(reader);
			case getAllDishs:
				return parseGetAllDishs(reader);
			case getUpdateDishs:
				return parseGetUpdateDishs(reader);
			case login:
				return parseLogin(reader);
			case updateUserInfo:
				return parseStatusUpdateUserInfo(reader);
			case getPlaces:
				return parseGetPlaces(reader);
			case submitReservation:
				return parseSubmitReservation(reader);
			case getListReservation:
				return parseGetListReservation(reader);
			case addPreOrder:
				return parseAddPreOrder(reader);
			case closeReservation:
				return parseCloseReservation(reader);
			case updateReservation:
				return parseUpdateReservation(reader);
			case addPreOrderDetail:
				return parseAddPreOrderDetail(reader);
			case getListOrderAndDetail:
				return parseGetListOrderAndDetail(reader);
			case cancelPreOrder:
				return parseCancelPreOrder(reader);
			case editYourPreOrder:
				return parseEditYourPreOrder(reader);
			case getAboutUs:
				return parseGetAboutUs(reader);
			default:
				break;
			}
		}

		catch (UnsupportedEncodingException e) {
			Log.e(TAG, "parse" + e.toString());
		}

		return null;
	}

	@Override
	public DataResult parseGetCurrentTime(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		CurrentTime currentTime = new CurrentTime();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("restaurant")) {
					reader.beginObject();
					while (reader.hasNext()) {
						String subname = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (subname.equalsIgnoreCase("date")) {
							String date = reader.nextString();
							currentTime.setDate(date);
						} else if (subname.equalsIgnoreCase("time")) {
							String time = reader.nextString();
							currentTime.setTime(time);
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			currentTime = null;
			Log.e(TAG, "parseGetCurrentTime()", e);
		}
		result.setData(currentTime);
		return result;
	}

	@Override
	public DataResult parseGetAllPromotions(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		ArrayList<Promotion> listPromotions = new ArrayList<Promotion>();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("promotions")) {
					JsonToken jsonToken = reader.peek();
					String checkBegin = jsonToken.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin)) {
						Promotion pro = parsePromotion(reader);
						if (pro != null) {
							listPromotions.add(pro);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							Promotion pro = parsePromotion(reader);
							if (pro != null) {
								listPromotions.add(pro);
							}
							// else: error
						}
						reader.endArray();
					}
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseGetPromotion()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			listPromotions = null;
		}
		result.setData(listPromotions);
		return result;
	}

	private Promotion parsePromotion(JsonReader reader) {
		Promotion pro = new Promotion();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String subname = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (subname.equalsIgnoreCase("promotionID")) {
					String promotionID = reader.nextString();
					pro.setPromotionId(promotionID);
				} else if (subname.equalsIgnoreCase("promotionName")) {
					String promotionName = reader.nextString();
					pro.setPromotionName(promotionName);
				} else if (subname.equalsIgnoreCase("promotionDescription")) {
					String promotionDescription = reader.nextString();
					pro.setPromotionDescription(promotionDescription);
				} else if (subname.equalsIgnoreCase("promotionSologan")) {
					String promotionSologan = reader.nextString();
					pro.setPromotionSologan(promotionSologan);
				} else if (subname.equalsIgnoreCase("promotionDiscount")) {
					String promotionDiscount = reader.nextString();
					pro.setPromotionDiscount(promotionDiscount);
				} else if (subname.equalsIgnoreCase("promotionImageSlide")) {
					String promotionImageSlide = reader.nextString();
					pro.setPromotionImageSlide(promotionImageSlide);
				} else if (subname.equalsIgnoreCase("promotionImageThumbnail")) {
					String promotionImageThumbnail = reader.nextString();
					pro.setPromotionImageThumbnail(promotionImageThumbnail);
				} else if (subname.equalsIgnoreCase("promotionImageMain")) {
					String promotionImageMain = reader.nextString();
					pro.setPromotionImageMain(promotionImageMain);
				} else if (subname.equalsIgnoreCase("promotionDate")) {
					String promotionDate = reader.nextString();
					pro.setPromotionDate(promotionDate);
				} else if (subname.equalsIgnoreCase("promotionTime")) {
					String promotionTime = reader.nextString();
					pro.setPromotionTime(promotionTime);
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parsePromotion()", e);
			pro = null;
		}
		return pro;
	}

	// chua lam phan delete
	@Override
	public DataResult parseGetUpdatePromotion(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		PromotionForUpdate proUpdate = new PromotionForUpdate();
		ArrayList<Promotion> listPromotions = new ArrayList<Promotion>();
		ArrayList<String> promotionIdList = new ArrayList<String>();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("promotions")) {
					JsonToken jsonToken = reader.peek();
					String checkBegin = jsonToken.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin)) {
						Promotion pro = parsePromotion(reader);
						if (pro != null) {
							listPromotions.add(pro);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							Promotion pro1 = parsePromotion(reader);
							if (pro1 != null) {
								listPromotions.add(pro1);
							}
							// else: error
						}
						reader.endArray();
					}
				} else if (name.equalsIgnoreCase("deletepost")) {
					JsonToken jsonToken1 = reader.peek();
					String checkBegin1 = jsonToken1.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin1)) {
						String id = parseDeletePost(reader);
						if (id != null && !id.equalsIgnoreCase("")) {
							promotionIdList.add(id);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							String id1 = parseDeletePost(reader);
							if (id1 != null) {
								promotionIdList.add(id1);
							}
							// else: error
						}
						reader.endArray();
					}
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseGetUpdatePromotion()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			proUpdate = null;
			listPromotions = null;
			promotionIdList = null;
			result.setData(null);
			return result;
		}
		proUpdate.setPromotionList(listPromotions);
		proUpdate.setPromotionIdList(promotionIdList);
		result.setData(proUpdate);
		return result;
	}

	private String parseDeletePost(JsonReader reader) {
		String id = null;
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String subname = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (subname.equalsIgnoreCase("ID")) {
					id = reader.nextString();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseDeletePost()", e);
		}
		return id;
	}

	@Override
	public DataResult parseGetAllDishCategory(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		ArrayList<DishCategory> dishCategoryList = new ArrayList<DishCategory>();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("dish_category")) {
					JsonToken jsonToken = reader.peek();
					String checkBegin = jsonToken.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin)) {
						DishCategory dishCategory = parseDishCategory(reader);
						if (dishCategory != null) {
							dishCategoryList.add(dishCategory);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							DishCategory dishCategory = parseDishCategory(reader);
							if (dishCategory != null) {
								dishCategoryList.add(dishCategory);
							}
							// else: error
						}
						reader.endArray();
					}
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseGetAllDishCategory()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			dishCategoryList = null;
		}
		result.setData(dishCategoryList);
		return result;
	}

	private DishCategory parseDishCategory(JsonReader reader) {
		DishCategory dishCategory = new DishCategory();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String subname = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (subname.equalsIgnoreCase("dish_category_id")) {
					String dishCategoryId = reader.nextString();
					dishCategory.setDishCategoryId(dishCategoryId);
				} else if (subname.equalsIgnoreCase("dish_category_name")) {
					String dishCategoryName = reader.nextString();
					dishCategory.setDishCategoryName(dishCategoryName);
				} else if (subname.equalsIgnoreCase("dish_category_image")) {
					String dishCategoryImage = reader.nextString();
					dishCategory.setDishCategoryImage(dishCategoryImage);
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseDishCategory()", e);
			dishCategory = null;
		}
		return dishCategory;
	}

	@Override
	public DataResult parseGetAllDishs(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		ArrayList<Food> foodList = new ArrayList<Food>();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("food")) {
					JsonToken jsonToken = reader.peek();
					String checkBegin = jsonToken.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin)) {
						Food food = parseFood(reader);
						if (food != null) {
							foodList.add(food);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							Food food = parseFood(reader);
							if (food != null) {
								foodList.add(food);
							}
							// else: error
						}
						reader.endArray();
					}
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseGetAllDishs()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			foodList = null;
		}
		result.setData(foodList);
		return result;
	}

	private Food parseFood(JsonReader reader) {
		Food food = new Food();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String subname = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (subname.equalsIgnoreCase("foodID")) {
					String foodId = reader.nextString();
					food.setFoodID(foodId);
				} else if (subname.equalsIgnoreCase("foodName")) {
					String foodName = reader.nextString();
					food.setFoodName(foodName);
				} else if (subname.equalsIgnoreCase("foodDescription")) {
					String foodDescription = reader.nextString();
					food.setFoodDescription(foodDescription);
				} else if (subname.equalsIgnoreCase("foodImage")) {
					String foodImage = reader.nextString();
					food.setFoodImage(foodImage);
				} else if (subname.equalsIgnoreCase("foodPrice")) {
					String foodPrice = reader.nextString();
					food.setFoodPrice(foodPrice);
				} else if (subname.equalsIgnoreCase("foodTypeID")) {
					String foodTypeID = reader.nextString();
					food.setFoodTypeID(foodTypeID);
				} else if (subname.equalsIgnoreCase("foodStatus")) {
					String foodStatus = reader.nextString();
					food.setFoodStatus(foodStatus);
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseFood()", e);
			food = null;
		}
		return food;
	}

	@Override
	public DataResult parseGetUpdateDishs(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		FoodForUpdate foodUpdate = new FoodForUpdate();
		ArrayList<Food> foodList = new ArrayList<Food>();
		ArrayList<String> foodIdList = new ArrayList<String>();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("food")) {
					JsonToken jsonToken = reader.peek();
					String checkBegin = jsonToken.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin)) {
						Food food = parseFood(reader);
						if (food != null) {
							foodList.add(food);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							Food food1 = parseFood(reader);
							if (food1 != null) {
								foodList.add(food1);
							}
							// else: error
						}
						reader.endArray();
					}
				} else if (name.equalsIgnoreCase("deletepost")) {
					JsonToken jsonToken1 = reader.peek();
					String checkBegin1 = jsonToken1.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin1)) {
						String id = parseDeletePost(reader);
						if (id != null && !id.equalsIgnoreCase("")) {
							foodIdList.add(id);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							String id1 = parseDeletePost(reader);
							if (id1 != null) {
								foodIdList.add(id1);
							}
							// else: error
						}
						reader.endArray();
					}
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseGetUpdatePromotion()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			foodUpdate = null;
			foodList = null;
			foodIdList = null;
			result.setData(null);
			return result;
		}
		foodUpdate.setFoodList(foodList);
		foodUpdate.setFoodIdList(foodIdList);
		result.setData(foodUpdate);
		return result;
	}

	@Override
	public DataResult parseLogin(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		UserInfo userInfo = new UserInfo();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("customer")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("user_id")) {
							userInfo.setUserId(reader.nextString());
						} else if (name.equalsIgnoreCase("generate_id")) {
							userInfo.setGenerateId(reader.nextString());
						} else if (name.equalsIgnoreCase("customer_name")) {
							userInfo.setCustomerName(reader.nextString());
						} else if (name.equalsIgnoreCase("customer_email")) {
							userInfo.setCustomerEmail(reader.nextString());
							// reader.skipValue();
						} else if (name.equalsIgnoreCase("customer_address")) {
							userInfo.setCustomerAddress(reader.nextString());
						} else if (name.equalsIgnoreCase("customer_avata")) {
							userInfo.setCustomerAvataURL(reader.nextString());
						} else if (name.equalsIgnoreCase("customer_phone")) {
							userInfo.setCustomerPhone(reader.nextString());
							// reader.skipValue();
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseLogin()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			userInfo = null;
		}
		result.setData(userInfo);
		return result;
	}

	@Override
	public DataResult parseStatusUpdateUserInfo(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("restaurant")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("string")) {
							result.setData(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseLogin()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			result.setData("false");
		}
		return result;
	}

	@Override
	public DataResult parseGetPlaces(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		ArrayList<Place> placeList = new ArrayList<Place>();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("places")) {
					JsonToken jsonToken = reader.peek();
					String checkBegin = jsonToken.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin)) {
						Place place = parsePlace(reader);
						if (place != null) {
							placeList.add(place);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							Place place = parsePlace(reader);
							if (place != null) {
								placeList.add(place);
							}
							// else: error
						}
						reader.endArray();
					}
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseGetPlaces()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			placeList = null;
		}
		result.setData(placeList);
		return result;

	}

	private Place parsePlace(JsonReader reader) {
		Place place = new Place();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String subname = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (subname.equalsIgnoreCase("placeID")) {
					String placeID = reader.nextString();
					place.setPlaceID(placeID);
				} else if (subname.equalsIgnoreCase("placeName")) {
					String placeName = reader.nextString();
					place.setPlaceName(placeName);
				} else if (subname.equalsIgnoreCase("placeDescription")) {
					String placeDescription = reader.nextString();
					place.setPlaceDescription(placeDescription);
				} else if (subname.equalsIgnoreCase("placeShortIntro")) {
					String placeShortIntro = reader.nextString();
					place.setPlaceShortIntro(placeShortIntro);
				} else if (subname.equalsIgnoreCase("placeAddress")) {
					String placeAddress = reader.nextString();
					place.setPlaceAddress(placeAddress);
				} else if (subname.equalsIgnoreCase("placePhone")) {
					String placePhone = reader.nextString();
					place.setPlacePhone(placePhone);
				} else if (subname.equalsIgnoreCase("placeOpen")) {
					String placeOpen = reader.nextString();
					place.setPlaceOpen(placeOpen);
				} else if (subname.equalsIgnoreCase("placeClose")) {
					String placeClose = reader.nextString();
					place.setPlaceClose(placeClose);
				} else if (subname.equalsIgnoreCase("placeLat")) {
					String placeLat = reader.nextString();
					place.setPlaceLat(placeLat);
				} else if (subname.equalsIgnoreCase("placeLong")) {
					String placeLong = reader.nextString();
					place.setPlaceLong(placeLong);
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parsePlace()", e);
			place = null;
		}
		return place;
	}

	@Override
	public DataResult parseSubmitReservation(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("reservation")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("string")) {
							result.setData(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseSubmitReservation()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			result.setData("false");
		}
		return result;
	}

	@Override
	public DataResult parseCloseReservation(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("restaurant")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("string")) {
							result.setData(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseSubmitReservation()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			result.setData("false");
		}
		return result;
	}

	@Override
	public DataResult parseUpdateReservation(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("restaurant")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("string")) {
							result.setData(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseSubmitReservation()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			result.setData("false");
		}
		return result;
	}

	@Override
	public DataResult parseGetListReservation(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		List<Reservation> reservations = new ArrayList<Reservation>();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("reservation")) {
					JsonToken token = reader.peek();
					if (token == JsonToken.BEGIN_ARRAY) {
						reader.beginArray();
						while (reader.hasNext()) {
							reader.beginObject();
							reservations.add(parseReservation(reader));
							reader.endObject();
						}
						reader.endArray();
					} else {
						reader.beginObject();
						reservations.add(parseReservation(reader));
						reader.endObject();
					}
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseLogin()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			result.setData("false");
		}
		result.setData(reservations);
		return result;
	}

	private Reservation parseReservation(JsonReader reader) {
		String name = "";
		Reservation reservation = new Reservation();
		try {
			while (reader.hasNext()) {
				name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("reservationID")) {
					reservation.setReservationId(reader.nextString());
				} else if (name.equalsIgnoreCase("reservationDate")) {
					reservation.setReservationDate(reader.nextString());
				} else if (name.equalsIgnoreCase("reservationTime")) {
					reservation.setReservationTime(reader.nextString());
				} else if (name.equalsIgnoreCase("reservationNumber")) {
					reservation.setReservationNumber(reader.nextString());
				} else if (name.equalsIgnoreCase("reservationMembercode")) {
					reservation.setReservationMemberCode(reader.nextString());
				} else if (name.equalsIgnoreCase("reservationStatus")) {
					reservation.setReservationStatus(reader.nextString());
				} else {
					reader.skipValue();
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return null;
		}
		return reservation;
	}

	@Override
	public DataResult parseAddPreOrder(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("preorder")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("order_id")) {
							result.setData(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseAddPreOrder()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			// result.setData(null);
		}
		return result;
	}

	@Override
	public DataResult parseAddPreOrderDetail(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("preorder")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("string")) {
							result.setData(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseAddPreOrderDetail()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			result.setData("false");
		}
		return result;
	}

	@Override
	public DataResult parseGetListOrderAndDetail(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		ArrayList<PreOrderObject> preOrderList = new ArrayList<PreOrderObject>();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("preorder")) {
					JsonToken jsonToken = reader.peek();
					String checkBegin = jsonToken.toString();
					// Check json is begin_object
					if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin)) {
						PreOrderObject preOrder = parsePreOrderObject(reader);
						if (preOrder != null) {
							preOrderList.add(preOrder);
						}
						// else: error
					} else {// json is begin_array
						reader.beginArray();
						while (reader.hasNext()) {
							PreOrderObject preOrder = parsePreOrderObject(reader);
							if (preOrder != null) {
								preOrderList.add(preOrder);
							}
							// else: error
						}
						reader.endArray();
					}
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseGetListOrderAndDetail()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			preOrderList = null;
		}
		result.setData(preOrderList);
		return result;
	}

	private PreOrderObject parsePreOrderObject(JsonReader reader) {
		PreOrderObject preOrderObject = new PreOrderObject();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String subname = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (subname.equalsIgnoreCase("preorderID")) {
					String preorderID = reader.nextString();
					preOrderObject.setPreorderID(preorderID);
				} else if (subname.equalsIgnoreCase("preorderMembercode")) {
					String preorderMembercode = reader.nextString();
					preOrderObject.setPreorderMembercode(preorderMembercode);
				} else if (subname.equalsIgnoreCase("preorderDate")) {
					String preorderDate = reader.nextString();
					preOrderObject.setPreorderDate(preorderDate);
				} else if (subname.equalsIgnoreCase("preorderTime")) {
					String preorderTime = reader.nextString();
					preOrderObject.setPreorderTime(preorderTime);
				} else if (subname.equalsIgnoreCase("preorderNumber")) {
					String preorderNumber = reader.nextString();
					preOrderObject.setPreorderNumber(preorderNumber);
				} else if (subname.equalsIgnoreCase("preorderPlaceID")) {
					String preorderPlaceID = reader.nextString();
					preOrderObject.setPreorderPlaceID(preorderPlaceID);
				} else if (subname.equalsIgnoreCase("preorderPlaceName")) {
					String preorderPlaceName = reader.nextString();
					preOrderObject.setPreorderPlaceName(preorderPlaceName);
				} else if (subname.equalsIgnoreCase("preorderPlaceAddress")) {
					String preorderPlaceAddress = reader.nextString();
					preOrderObject
							.setPreorderPlaceAddress(preorderPlaceAddress);
				} else if (subname.equalsIgnoreCase("preorderStatus")) {
					String preorderStatus = reader.nextString();
					preOrderObject.setPreorderStatus(preorderStatus);
				} else if (subname.equalsIgnoreCase("orderdetail")) {
					preOrderObject.setOrderdetail(parseOrderDetailList(reader));
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parsePreOrderObject()", e);
			preOrderObject = null;
		}
		return preOrderObject;
	}

	private ArrayList<Food> parseOrderDetailList(JsonReader reader) {
		// TODO Auto-generated method stub
		ArrayList<Food> foodList = new ArrayList<Food>();
		try {
			JsonToken jsonToken = reader.peek();
			String checkBegin = jsonToken.toString();
			// Check json is begin_object
			if ("BEGIN_OBJECT".equalsIgnoreCase(checkBegin)) {
				Food food = parseOrderDetailObject(reader);
				if (food != null) {
					foodList.add(food);
				}
				// else: error
			} else {// json is begin_array
				reader.beginArray();
				while (reader.hasNext()) {
					Food food = parseOrderDetailObject(reader);
					if (food != null) {
						foodList.add(food);
					}
					// else: error
				}
				reader.endArray();
			}

		} catch (IOException e) {
			Log.e(TAG, "parseOrderDetail()" + e.toString());
			foodList = null;
		}
		return foodList;
	}

	private Food parseOrderDetailObject(JsonReader reader) {
		Food food = new Food();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String subname = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (subname.equalsIgnoreCase("foodID")) {
					String foodId = reader.nextString();
					food.setFoodID(foodId);
				} else if (subname.equalsIgnoreCase("foodName")) {
					String foodName = reader.nextString();
					food.setFoodName(foodName);
				} else if (subname.equalsIgnoreCase("foodPrice")) {
					String foodPrice = reader.nextString();
					food.setFoodPrice(foodPrice);
				} else if (subname.equalsIgnoreCase("foodImage")) {
					String foodImage = reader.nextString();
					food.setFoodImage(foodImage);
				} else if (subname.equalsIgnoreCase("foodAmount")) {
					String foodAmount = reader.nextString();
					food.setFoodAmount(Integer.parseInt(foodAmount));
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseOrderDetailObject()", e);
			food = null;
		}
		return food;
	}

	@Override
	public DataResult parseCancelPreOrder(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("preorder")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("string")) {
							result.setData(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseCancelPreOrder()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			result.setData("false");
		}
		return result;
	}
	
	@Override
	public DataResult parseEditYourPreOrder(JsonReader reader) {
		// TODO Auto-generated method stub
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("restaurant")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("string")) {
							result.setData(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseEditYourPreOrder()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
			result.setData("false");
		}
		return result;
	}
	@Override
	public DataResult parseGetAboutUs(JsonReader reader) {
		// TODO Auto-generated method stub
		AboutUs aboutUs = new AboutUs();
		DataResult result = new DataResult();
		try {
			reader.beginObject();
			while (reader.hasNext()) {
				String name = reader.nextName();
				if (reader.peek() == JsonToken.NULL) {
					reader.skipValue();
				} else if (name.equalsIgnoreCase("aboutus")) {
					reader.beginObject();
					while (reader.hasNext()) {
						name = reader.nextName();
						if (reader.peek() == JsonToken.NULL) {
							reader.skipValue();
						} else if (name.equalsIgnoreCase("aboutusName")) {
							aboutUs.setTitle(reader.nextString());
						} else if (name.equalsIgnoreCase("aboutusDescription")) {
							aboutUs.setContent(reader.nextString());
						} else if (name.equalsIgnoreCase("aboutusImage")) {
							aboutUs.setImageURL(reader.nextString());
						} else {
							reader.skipValue();
						}
					}
					reader.endObject();
				} else {
					reader.skipValue();
				}
			}
			reader.endObject();
		} catch (IOException e) {
			Log.e(TAG, "parseGetAboutUs()" + e.toString());
			result.setErrorCode(ErrorCode.SERVER_ERROR);
		}
		result.setData(aboutUs);
		return result;
	}

}
